package com.xingda.packing.minitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingda.packing.minitor.entity.PkBom;
import com.xingda.packing.minitor.entity.PkTaskBom;
import com.xingda.packing.minitor.mapper.PkBomMapper;
import com.xingda.packing.minitor.mapper.PkTaskBomMapper;
import com.xingda.packing.minitor.service.IPkTaskBomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PkTaskBomServiceImpl extends ServiceImpl<PkTaskBomMapper, PkTaskBom> implements IPkTaskBomService {

    @Resource
    PkBomMapper pkBomMapper;

    @Transactional
    @Override
    public void saveTaskBom(int taskId, List<PkBom> pkBoms) {

        if (CollectionUtils.isEmpty(pkBoms)) {
            return;
        }
        for (PkBom bom : pkBoms) {
            LambdaQueryWrapper<PkBom> pkBomQueryWrapper = Wrappers.lambdaQuery();
            pkBomQueryWrapper.eq(PkBom::getPkInvbasdoc, bom.getPkInvbasdoc());
            List<PkBom> pkbomList = pkBomMapper.selectList(pkBomQueryWrapper);
            if (CollectionUtils.isEmpty(pkbomList)) {
                pkBomMapper.insert(bom);
            }
            PkTaskBom pkTaskBom = new PkTaskBom();
            pkTaskBom.setPkBomPkInvbasdoc(bom.getPkInvbasdoc());
            pkTaskBom.setPkTaskId(taskId);
            this.save(pkTaskBom);
        }
    }

}
